<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>十进制转二进制</title>
  <script>

      //定义一个构造函数(栈)
      function Stack() {
          this.items = [];
          //1.实现元素输入
          Stack.prototype.push = function (element) {
              this.items.push(element);
          }
          //2.实现元素弹出
          Stack.prototype.pop = function (){
              this.items.pop();
          }
          //3.实现元素的读取
          Stack.prototype.peek = function () {
              return this.items[this.items.length - 1];
          }
          //4.判断栈是否为空
          Stack.prototype.isEmpty = function () {
              return this.items.length == 0;
          }
          //5.获取栈中的元素个数
          Stack.prototype.size = function () {
              return this.items.length;
          }
          //6.toString方法
          Stack.prototype.toString = function () {
              var resultString = '';
              for (var i = 0; i < this.items.length; i++) {
                  resultString += this.items[i] + '';
              }
              return resultString;
          }

      }

      //十进制转二进制
      function desToTwo(desNum) {
          //new 一个栈，用来存放数据
          var  stack = new Stack();
          var num ;
          while (desNum >= 1){
              num = desNum % 2;
              desNum = parseInt(desNum/2);
              stack.push(num);
          }

          var binaryString = '';
          while(!stack.isEmpty()){
              binaryString += stack.pop();
          }
          return binaryString;
      }

      console.log(desToTwo(10));
      alert(desToTwo(10));

  </script>
</head>
<body>

</body>
</html>